package com.lineage.echo;

import com.lineage.config.Config;
import com.lineage.echo.encryptions.PacketPrint;
import com.lineage.server.clientpackets.C_AddBookmark;
import com.lineage.server.clientpackets.C_AddBuddy;
import com.lineage.server.clientpackets.C_Amount;
import com.lineage.server.clientpackets.C_Attack;
import com.lineage.server.clientpackets.C_AttackBow;
import com.lineage.server.clientpackets.C_Attr;
import com.lineage.server.clientpackets.C_AuthLogin;
import com.lineage.server.clientpackets.C_AutoLogin;
import com.lineage.server.clientpackets.C_BanClan;
import com.lineage.server.clientpackets.C_BanParty;
import com.lineage.server.clientpackets.C_Board;
import com.lineage.server.clientpackets.C_BoardBack;
import com.lineage.server.clientpackets.C_BoardDelete;
import com.lineage.server.clientpackets.C_BoardRead;
import com.lineage.server.clientpackets.C_BoardWrite;
import com.lineage.server.clientpackets.C_Buddy;
import com.lineage.server.clientpackets.C_CallPlayer;
import com.lineage.server.clientpackets.C_ChangeHeading;
import com.lineage.server.clientpackets.C_ChangeWarTime;
import com.lineage.server.clientpackets.C_CharReset;
import com.lineage.server.clientpackets.C_CharcterConfig;
import com.lineage.server.clientpackets.C_Chat;
import com.lineage.server.clientpackets.C_ChatGlobal;
import com.lineage.server.clientpackets.C_ChatParty;
import com.lineage.server.clientpackets.C_ChatWhisper;
import com.lineage.server.clientpackets.C_CheckPK;
import com.lineage.server.clientpackets.C_ClanMatching;
import com.lineage.server.clientpackets.C_CommonClick;
import com.lineage.server.clientpackets.C_CreateChar;
import com.lineage.server.clientpackets.C_CreateClan;
import com.lineage.server.clientpackets.C_CreateParty;
import com.lineage.server.clientpackets.C_DelBuddy;
import com.lineage.server.clientpackets.C_DeleteBookmark;
import com.lineage.server.clientpackets.C_DeleteChar;
import com.lineage.server.clientpackets.C_DeleteInventoryItem;
import com.lineage.server.clientpackets.C_Deposit;
import com.lineage.server.clientpackets.C_Disconnect;
import com.lineage.server.clientpackets.C_Door;
import com.lineage.server.clientpackets.C_Drawal;
import com.lineage.server.clientpackets.C_DropItem;
import com.lineage.server.clientpackets.C_EmblemDownload;
import com.lineage.server.clientpackets.C_EmblemUpload;
import com.lineage.server.clientpackets.C_EnterPortal;
import com.lineage.server.clientpackets.C_Exclude;
import com.lineage.server.clientpackets.C_ExitGhost;
import com.lineage.server.clientpackets.C_ExtraCommand;
import com.lineage.server.clientpackets.C_ExtraPacket;
import com.lineage.server.clientpackets.C_Fight;
import com.lineage.server.clientpackets.C_FishClick;
import com.lineage.server.clientpackets.C_FixWeaponList;
import com.lineage.server.clientpackets.C_GiveItem;
import com.lineage.server.clientpackets.C_HireSoldier;
import com.lineage.server.clientpackets.C_ItemUSe;
import com.lineage.server.clientpackets.C_JoinClan;
import com.lineage.server.clientpackets.C_KeepALIVE;
import com.lineage.server.clientpackets.C_LeaveClan;
import com.lineage.server.clientpackets.C_LeaveParty;
import com.lineage.server.clientpackets.C_LoginToServer;
import com.lineage.server.clientpackets.C_LoginToServerOK;
import com.lineage.server.clientpackets.C_Mail;
import com.lineage.server.clientpackets.C_MoveChar;
import com.lineage.server.clientpackets.C_NPCAction;
import com.lineage.server.clientpackets.C_NPCTalk;
import com.lineage.server.clientpackets.C_NewCharSelect;
import com.lineage.server.clientpackets.C_Party;
import com.lineage.server.clientpackets.C_Password;
import com.lineage.server.clientpackets.C_PetMenu;
import com.lineage.server.clientpackets.C_PickUpItem;
import com.lineage.server.clientpackets.C_Pledge;
import com.lineage.server.clientpackets.C_PledgeContent;
import com.lineage.server.clientpackets.C_Propose;
import com.lineage.server.clientpackets.C_Rank;
import com.lineage.server.clientpackets.C_Restart;
import com.lineage.server.clientpackets.C_Result;
import com.lineage.server.clientpackets.C_SelectList;
import com.lineage.server.clientpackets.C_SelectTarget;
import com.lineage.server.clientpackets.C_ServerVersion;
import com.lineage.server.clientpackets.C_Ship;
import com.lineage.server.clientpackets.C_Shop;
import com.lineage.server.clientpackets.C_ShopList;
import com.lineage.server.clientpackets.C_SkillBuy;
import com.lineage.server.clientpackets.C_SkillBuyItem;
import com.lineage.server.clientpackets.C_SkillBuyOK;
import com.lineage.server.clientpackets.C_TaxRate;
import com.lineage.server.clientpackets.C_Teleport;
import com.lineage.server.clientpackets.C_Title;
import com.lineage.server.clientpackets.C_Trade;
import com.lineage.server.clientpackets.C_TradeAddItem;
import com.lineage.server.clientpackets.C_TradeCancel;
import com.lineage.server.clientpackets.C_TradeOK;
import com.lineage.server.clientpackets.C_UsePetItem;
import com.lineage.server.clientpackets.C_UseSkill;
import com.lineage.server.clientpackets.C_War;
import com.lineage.server.clientpackets.C_Who;
import com.lineage.server.clientpackets.C_Windows;
import com.lineage.server.clientpackets.ClientBasePacket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PacketHandler extends PacketHandlerExecutor {
  private static final Log _log = LogFactory.getLog(PacketHandler.class);
  
  private static final Map<Integer, ClientBasePacket> _opListClient = new HashMap<>();
  
  private final ClientExecutor _client;
  
  public PacketHandler(ClientExecutor client) {
    this._client = client;
  }
  
  public static void load() {
    put(Integer.valueOf(1), (ClientBasePacket)new C_ExtraPacket());
    put(Integer.valueOf(98), (ClientBasePacket)new C_CharReset());
    put(Integer.valueOf(122), (ClientBasePacket)new C_Disconnect());
    put(Integer.valueOf(171), (ClientBasePacket)new C_Exclude());
    put(Integer.valueOf(244), (ClientBasePacket)new C_CharcterConfig());
    put(Integer.valueOf(41), (ClientBasePacket)new C_Door());
    put(Integer.valueOf(90), (ClientBasePacket)new C_Title());
    put(Integer.valueOf(153), (ClientBasePacket)new C_BoardDelete());
    put(Integer.valueOf(68), (ClientBasePacket)new C_Pledge());
    put(Integer.valueOf(225), (ClientBasePacket)new C_ChangeHeading());
    put(Integer.valueOf(125), (ClientBasePacket)new C_NPCAction());
    put(Integer.valueOf(6), (ClientBasePacket)new C_UseSkill());
    put(Integer.valueOf(18), (ClientBasePacket)new C_EmblemUpload());
    put(Integer.valueOf(86), (ClientBasePacket)new C_TradeCancel());
    put(Integer.valueOf(1443), (ClientBasePacket)new C_ChangeWarTime());
    put(Integer.valueOf(165), (ClientBasePacket)new C_AddBookmark());
    put(Integer.valueOf(222), (ClientBasePacket)new C_CreateClan());
    put(Integer.valueOf(14), (ClientBasePacket)new C_ServerVersion());
    put(Integer.valueOf(50), (ClientBasePacket)new C_Propose());
    put(Integer.valueOf(23), (ClientBasePacket)new C_BoardBack());
    put(Integer.valueOf(38), (ClientBasePacket)new C_Shop());
    put(Integer.valueOf(114), (ClientBasePacket)new C_BoardRead());
    put(Integer.valueOf(2), (ClientBasePacket)new C_Trade());
    put(Integer.valueOf(162), (ClientBasePacket)new C_DeleteChar());
    put(Integer.valueOf(121), (ClientBasePacket)new C_Attr());
    put(Integer.valueOf(119), (ClientBasePacket)new C_AuthLogin());
    put(Integer.valueOf(161), (ClientBasePacket)new C_Result());
    put(Integer.valueOf(56), (ClientBasePacket)new C_Deposit());
    put(Integer.valueOf(44), (ClientBasePacket)new C_Drawal());
    put(Integer.valueOf(26), (ClientBasePacket)new C_LoginToServerOK());
    put(Integer.valueOf(145), (ClientBasePacket)new C_SkillBuy());
    put(Integer.valueOf(39), (ClientBasePacket)new C_SkillBuyOK());
    put(Integer.valueOf(245), (ClientBasePacket)new C_SkillBuyItem());
    put(Integer.valueOf(37), (ClientBasePacket)new C_TradeAddItem());
    put(Integer.valueOf(207), (ClientBasePacket)new C_AddBuddy());
    put(Integer.valueOf(136), (ClientBasePacket)new C_Chat());
    put(Integer.valueOf(71), (ClientBasePacket)new C_TradeOK());
    put(Integer.valueOf(51), (ClientBasePacket)new C_CheckPK());
    put(Integer.valueOf(19), (ClientBasePacket)new C_TaxRate());
    put(Integer.valueOf(7), (ClientBasePacket)new C_NewCharSelect());
    put(Integer.valueOf(4), (ClientBasePacket)new C_Buddy());
    put(Integer.valueOf(25), (ClientBasePacket)new C_DropItem());
    put(Integer.valueOf(33), (ClientBasePacket)new C_LeaveParty());
    put(Integer.valueOf(229), (ClientBasePacket)new C_Attack());
    put(Integer.valueOf(123), (ClientBasePacket)new C_AttackBow());
    put(Integer.valueOf(69), (ClientBasePacket)new C_BanClan());
    put(Integer.valueOf(10), (ClientBasePacket)new C_Board());
    put(Integer.valueOf(138), (ClientBasePacket)new C_DeleteInventoryItem());
    put(Integer.valueOf(184), (ClientBasePacket)new C_ChatWhisper());
    put(Integer.valueOf(43), (ClientBasePacket)new C_Party());
    put(Integer.valueOf(112), (ClientBasePacket)new C_PickUpItem());
    put(Integer.valueOf(206), (ClientBasePacket)new C_Who());
    put(Integer.valueOf(45), (ClientBasePacket)new C_GiveItem());
    put(Integer.valueOf(29), (ClientBasePacket)new C_MoveChar());
    put(Integer.valueOf(3), (ClientBasePacket)new C_DeleteBookmark());
    put(Integer.valueOf(177), (ClientBasePacket)new C_Restart());
    put(Integer.valueOf(61), (ClientBasePacket)new C_LeaveClan());
    put(Integer.valueOf(34), (ClientBasePacket)new C_NPCTalk());
    put(Integer.valueOf(255), (ClientBasePacket)new C_BanParty());
    put(Integer.valueOf(202), (ClientBasePacket)new C_DelBuddy());
    put(Integer.valueOf(227), (ClientBasePacket)new C_War());
    put(Integer.valueOf(137), (ClientBasePacket)new C_LoginToServer());
    put(Integer.valueOf(47), (ClientBasePacket)new C_ShopList());
    put(Integer.valueOf(40), (ClientBasePacket)new C_ChatGlobal());
    put(Integer.valueOf(194), (ClientBasePacket)new C_JoinClan());
    put(Integer.valueOf(16), (ClientBasePacket)new C_CommonClick());
    put(Integer.valueOf(84), (ClientBasePacket)new C_CreateChar());
    put(Integer.valueOf(120), (ClientBasePacket)new C_ExtraCommand());
    put(Integer.valueOf(141), (ClientBasePacket)new C_BoardWrite());
    put(Integer.valueOf(164), (ClientBasePacket)new C_ItemUSe());
    put(Integer.valueOf(230), (ClientBasePacket)new C_CreateParty());
    put(Integer.valueOf(219), (ClientBasePacket)new C_EnterPortal());
    put(Integer.valueOf(11), (ClientBasePacket)new C_Amount());
    put(Integer.valueOf(118), (ClientBasePacket)new C_FixWeaponList());
    put(Integer.valueOf(20), (ClientBasePacket)new C_SelectList());
    put(Integer.valueOf(173), (ClientBasePacket)new C_ExitGhost());
    put(Integer.valueOf(185), (ClientBasePacket)new C_CallPlayer());
    put(Integer.valueOf(223), (ClientBasePacket)new C_SelectTarget());
    put(Integer.valueOf(103), (ClientBasePacket)new C_PetMenu());
    put(Integer.valueOf(104), (ClientBasePacket)new C_UsePetItem());
    put(Integer.valueOf(63), (ClientBasePacket)new C_Rank());
    put(Integer.valueOf(199), (ClientBasePacket)new C_ChatParty());
    put(Integer.valueOf(5), (ClientBasePacket)new C_Fight());
    put(Integer.valueOf(87), (ClientBasePacket)new C_Mail());
    put(Integer.valueOf(231), (ClientBasePacket)new C_Ship());
    put(Integer.valueOf(72), (ClientBasePacket)new C_EmblemDownload());
    put(Integer.valueOf(52), (ClientBasePacket)new C_Teleport());
    put(Integer.valueOf(246), (ClientBasePacket)new C_Teleport());
    put(Integer.valueOf(13), (ClientBasePacket)new C_Password());
    put(Integer.valueOf(95), (ClientBasePacket)new C_KeepALIVE());
    put(Integer.valueOf(254), (ClientBasePacket)new C_Windows());
    put(Integer.valueOf(210), (ClientBasePacket)new C_AutoLogin());
    put(Integer.valueOf(62), (ClientBasePacket)new C_FishClick());
    put(Integer.valueOf(1411), (ClientBasePacket)new C_HireSoldier());
    put(Integer.valueOf(76), (ClientBasePacket)new C_ClanMatching());
    put(Integer.valueOf(78), (ClientBasePacket)new C_PledgeContent());
    put(Integer.valueOf(99), (ClientBasePacket)new C_Mail());
  }
  
  public static void put(Integer key, ClientBasePacket value) {
    if (_opListClient.get(key) == null) {
      _opListClient.put(key, value);
    } else if (!key.equals(Integer.valueOf(-1))) {
      _log.error("重复标记的OPID: " + key + " " + value.getType());
    } 
  }
  
  public void handlePacket(byte[] decrypt) {
    ClientBasePacket basePacket = null;
    if (decrypt == null)
      return; 
    if (decrypt.length <= 0)
      return; 
    try {
      int key = decrypt[0] & 0xFF;
      basePacket = _opListClient.get(Integer.valueOf(key));
      if (Config.DEBUG && 
        basePacket != null)
        _log.info("客户端: " + basePacket.getType() + 
            "\nOP ID: " + key + " length:" + decrypt.length + 
            "\nInfo:\n" + PacketPrint.get().printData(decrypt, decrypt.length)); 
      if (basePacket == null) {
        _log.info("\nClient: " + key + 
            "\n" + PacketPrint.get().printData(decrypt, decrypt.length) + 
            getNow_YMDHMS());
      } else {
        basePacket.start(decrypt, this._client);
      } 
    } catch (Exception e) {
      if (Config.DEBUG) {
        String name = "Not Login Pc";
        if (this._client.getActiveChar() != null)
          name = this._client.getActiveChar().getName(); 
        _log.error("OP ID: " + (
            decrypt[0] & 0xFF) + 
            " Pc Name: " + 
            name + 
            "\n" + 
            basePacket.getType() + 
            "\n" + 
            PacketPrint.get().printData(decrypt, decrypt.length), e);
      } 
    } finally {
      basePacket = null;
    } 
  }
  
  private String getNow_YMDHMS() {
    String nowDate = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date());
    return nowDate;
  }
}
